"""This module defines the PyMak Rule class.
"""
# Python imports

# Package imports
import path
import variant
import target

__all__ = [
    'Rule',
    ]



class Rule(object):
    """This class implements the abstract base class for a rule. A
    Rule defines a mechanism for creating Target(s). Classes that
    derive from Rule should implment the 'apply_rule' method. The
    implementation in this class just creates a vanilla Target object,
    passing the the kwargs through to the constructor.
    """
    def __init__(self, context):
        self.context = context

    def apply_rule(self, tpath, **kwargs):
        """ This method is called when the rule needs to be applied,
        and is normally overridden by derived classes. The tpath
        parameter is the name of the target that has been specified by
        a makefile fragment, for example foo.cpp or bar.exe.  The rest
        of the arguments are as for the Target constructor.  This
        method must return an instance of Target.
        """
        return target.Target(self.context, tpath, **kwargs)

    def update_target(self, target, **kwargs):
        """This method is rarely overridden by derived classes. It
        gets called when the context's target factory has an existing
        target for the supplied path, and the rule is also specified
        to the factory. The default implementation just calls
        set(**kwargs) on the target.
        """
        return target.set(**kwargs)


